package com.wowangz.cms.systemmanage.order.api;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.ServletUtils;
import com.wowangz.cms.systemmanage.order.entity.TblOrder;
import com.wowangz.cms.systemmanage.order.service.OrderService;

@Scope("prototype")
@Controller
public class OrderController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(OrderController.class);

	/**
	 * 点击菜单"商家优惠销售情况"显示“商家优惠销售情况”列表
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/systemmanage/order/queryChannelOrderList.web")
	public String queryChannelInfoList(HttpServletRequest request, HttpServletResponse response, Model model) {		
		
		return "/WEB-INF/jsp/systemmanage/order/channelOrderList";
	}

	/**
	 * 打开“商家优惠销售情况”列表加载列表数据
	 * 
	 * @param request
	 * @param response
	 * @param model
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/systemmanage/order/queryChannelOrderListInfoDataGrid.json")
	public void queryChannelOrderListInfoDataGrid(HttpServletRequest request, HttpServletResponse response, Model model) {
		PangingParam pangingParam = null;
		DataResult dataResult = new DataResult();
		DataGrid dataGrid = null;
		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			HashMap<String, Object> map = (HashMap) ServletUtils.getParametersToHashMap(request);
			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);
			map.put("startrow", dataGrid.getStartrow());
			map.put("endrow", dataGrid.getEndrow());
			map.put("rows", dataGrid.getRows());
			map.put("sort", dataGrid.getSort());
			map.put("order", dataGrid.getOrder());

			OrderService orderService = (OrderService) IssTransactionContainer.getInstance(OrderService.class, false);		
			long resultCount = orderService.findChannelOrderCount(map);
			List<TblOrder> resultList = orderService.findChannelOrderList(map);
	
			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(resultCount);
			dataStyle.setDataList(resultList);

			dataStyle.addDepict("salesman", DataTypeConstant.STRING);
			dataStyle.addDepict("channelId", DataTypeConstant.STRING);
			dataStyle.addDepict("channelName", DataTypeConstant.STRING);
			dataStyle.addDepict("channelCode", DataTypeConstant.STRING);
			dataStyle.addDepict("userId", DataTypeConstant.STRING);
			dataStyle.addDepict("orderCode", DataTypeConstant.STRING);
			dataStyle.addDepictByFunction("paymentType", SysConstant.PaymentType.class, "getName", new Class[] { String.class });
			dataStyle.addDepictByFunction("currencyType", SysConstant.CurrencyType.class, "getName", new Class[] { String.class });
			dataStyle.addDepict("orderMoney", DataTypeConstant.AMOUNT_2);
			dataStyle.addDepict("payMoney", DataTypeConstant.AMOUNT_2);
			dataStyle.addDepict("modifyTime", DataTypeConstant.DATETIME);
			dataResult = PangingTools.buildResultPagerInfo(dataStyle);
			if (pangingParam.getReportExportType() == 0) {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, getUserContext()));
			}
			logger.info("加载商家优惠销售情况列表数据成功");
		} catch (Exception e) {
			logger.error("加载商家优惠销售情况列表数据失败",e);
			e.printStackTrace();
			logger.debug(e.getMessage());
		}
	}
	
	

}
